﻿Imports System.Data.SqlServerCe

Public Class SharedWatchFolders
    Shared Sub New()
        Dim aSelectColumns As String() = {"Id", "Path", "Recursive", "GroupMode", "ItemTypes", "Name"}
        ObjectPersister.DefineObject(GetType(Entities.SharedWatchFolder), "SharedWatchFolders", aSelectColumns)
    End Sub
    Private Sub New()
    End Sub

    Public Shared Function GetAll() As Generic.List(Of Entities.SharedWatchFolder)
        Return ObjectPersister.GetAll(Of Entities.SharedWatchFolder)()
    End Function
    Public Shared Function GetById(ByVal id As Integer) As Entities.SharedWatchFolder
        Return ObjectPersister.GetOne(Of Entities.SharedWatchFolder)(String.Format("Id={0}", id))
    End Function

    Public Shared Sub Delete(ByVal id As Integer)
        Using t As New TransactionScope
            Dim strSql As String

            strSql = String.Format("DELETE FROM MediaInfo WHERE shareditemid in (select Id from SharedItems WHERE SourceWatchFolderId = {0})", id)
            Environment.ExecuteNonQuery(strSql, t)

            strSql = String.Format("DELETE FROM sharedItemsFiles where shareditemid in (select Id from SharedItems WHERE SourceWatchFolderId = {0})", id)
            Environment.ExecuteNonQuery(strSql, t)

            strSql = String.Format("DELETE FROM SharedItems WHERE SourceWatchFolderId = {0}", id)
            Environment.ExecuteNonQuery(strSql, t)

            strSql = String.Format("DELETE FROM SharedWatchFolders WHERE Id = {0}", id)
            If Environment.ExecuteNonQuery(strSql, t) <> 1 Then
                Throw New ApplicationException("No row was deleted!")
            End If

            t.Complete()
        End Using

    End Sub

End Class
